//
//  WBTabBar.swift
//  MyWeiBo
//
//  Created by mac on 15/3/5.
//  Copyright (c) 2015年 gpr. All rights reserved.
//

import UIKit

class WBTabBar: UITabBar {
    weak var _composeButton: UIButton?
    let _buttonCount = 5
    var composeButtonDidClickedClosure: (() -> ())?
    
    override func awakeFromNib() {
        let composeButton = UIButton()
        composeButton.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        composeButton.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        composeButton.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        composeButton.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        self.addSubview(composeButton)
        _composeButton = composeButton
        composeButton.addTarget(self, action: "compostButtonDidClicked", forControlEvents: UIControlEvents.TouchUpInside)
    }

    func compostButtonDidClicked() {
        if composeButtonDidClickedClosure != nil {
            composeButtonDidClickedClosure!()
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        var count = _buttonCount
        let h = self.bounds.size.height
        let w = self.bounds.size.width / CGFloat(count)
        var i = 0
        var buttonIndex = 0
        for item in self.subviews as! [UIView] {
            if item is UIControl && !(item is UIButton){
                item.frame = CGRectMake(CGFloat(i) * w, 0, w, h)
                i++
                if i == count / 2 {
                    buttonIndex = i
                    i++
                }
            }
        }
        (_composeButton! as UIView).frame = CGRectMake(CGFloat(buttonIndex) * w, 0, w, h)
    }

}
